/*
* 业务机会管理
* */
Ext.define('App.crm.businessOpportunitiesForm', {
    extend: 'Ext.window.Window',
    alias: 'widget.businessOpportunitiesForm',
    initComponent: function () {
        var form = Ext.widget('form', {
            frame: false,
            border: true,
            split: true,
            autoScroll: true,
            bodyPadding: 5,
            bodyBorder: true,
            fieldDefaults: {
                labelWidth: 80,
                labelAlign: 'right',
                msgTarget: 'side'
            },
            defaults: {
                anchor: '100%'
            },
            items: [
                {xtype:'hidden',name : 'id',
                    dataIndex : 'id'}, {xtype:'hidden',name : 'deleteFlag',
                    dataIndex : 'deleteFlag'},
                {
                    xtype: 'container',
                    layout: 'hbox',
                    margin: '0 0 5 0',
                    defaults: {
                        flex: 1
                    }, items: [{
                    xtype: 'container',
                    layout: 'hbox',
                    margin: '0 0 5 0',
                    defaults: {

                    }, items: [{xtype: 'hidden',
                        name: 'customerId',
                        dataIndex: 'customerId'
                    },
                        {xtype: 'textfield',
                            name: 'customerName',
                            dataIndex: 'customerName',
                            fieldLabel: '客户名称',
                            readOnly: true,
                            emailText: '请选择客户',
                            width: 240,
                            allowBlank: false
                        },
                        {
                            xtype: 'button',
                            name: 'btnCustomerId',
                            iconCls: 'searchIcon',
                            scope: this,
                            handler: this.onCustomersSel,
                            width: 25
                        }]}]},{
                    xtype: 'container',
                    layout: 'hbox',
                    margin: '0 0 5 0',
                    defaults: {
                        flex: 1
                    }, items: [{xtype:'textfield',	name : 'bussName',
                        dataIndex : 'bussName',
                        fieldLabel : '业务机会名',
                        allowBlank : false},new dateTime({
                        name : 'signDate',
                        dataIndex : 'signDate',
                        format:'Y-m-d H:i:s',
                        fieldLabel : '结束日期',
                        allowBlank : false
                    })]},{
                    xtype: 'container',
                    layout: 'hbox',
                    margin: '0 0 5 0',
                    defaults: {
                        flex: 1
                    }, items: [ {xtype:'textfield',name : 'phase',
                        dataIndex : 'phase',
                        fieldLabel : '阶段',
                        allowBlank : false},{xtype:'numberfield', name : 'chance',
                        dataIndex : 'chance',
                        allowDecimals : false,
                        decimalPrecision : null,
                        fieldLabel : '成交概率(%)',
                        allowBlank : true}]},{
                    xtype: 'container',
                    layout: 'hbox',
                    margin: '0 0 5 0',
                    defaults: {
                        flex: 1
                    }, items: [  new comboBox({
                        name: 'source',
                        dataIndex: 'source',
                        fieldLabel: '来源',
                        labelAlign: 'right',
                        codeName: '来源',
                        queryMode: 'local',
                        typeAhead: false,
                        forceSelection: true,
                        loadingText: '正在搜索...',
                        allowBlank: false,
                        hideTrigger: false
                    }),{xtype:'numberfield',    name : 'money',
                        dataIndex : 'money',
                        allowDecimals : true,
                        decimalPrecision : 2,
                        fieldLabel : '金额',
                        allowBlank : true}]},{
                    xtype: 'container',
                    layout: 'hbox',
                    margin: '0 0 5 0',
                    defaults: {
                        flex: 1
                    }, items: [{xtype:'textarea',	name : 'remark',
                        dataIndex : 'remark',
                        fieldLabel : '备注',
                        allowBlank : true},null]}
            ]
        });
        Ext.apply(this, {
            title: '新增',
            layout: 'fit',
            height: 400,
            width:600,
            modal: true,
            constrain: true,
            closeAction: 'hide',
            buttonAlign: 'center',
            buttons: [
                {
                    text: "保存",
                    handler: function () {
                        //this.up(‘window’)获取了顶级的window组件。接着使用down()方法获取了window的子组件form组件，最后使用getForm()来获取整个form。
                        var form = this.up('businessOpportunitiesForm').down('form');
                        if (form.getForm().isValid()) {
                            form.getForm().submit({
                                url: getRootPath() + '/businessOpportunitiesController/save.action',
                                scope: form,
                                success: function (form, action) {
                                    this.up('businessOpportunitiesForm').close();
                                    Ext.getCmp('businessOpportunitiesGrid').getStore().reload();
                                },
                                failure: function (form, action) {
                                    if (action.result.message == undefined)
                                        Ext.Msg.alert('错误', '网络连接失败.');
                                    else
                                        Ext.Msg.alert('错误', action.result.message);
                                },
                                waitMsg: '保存数据...'
                            });
                        } else {
                            Ext.Msg.alert('提示', '表单含有无效或空数据');
                        }
                    }
                },
                {
                    text: "关闭",
                    handler: function () {
                        this.up('businessOpportunitiesForm').close();
                    }
                }
            ],
            items: form
        });
        this.callParent(arguments);
    } ,
    onCustomersSel: function () {
       var customerWin= new customersSel();
        customerWin.on('onSelectItem', function(data) {
            this.down('form').getForm().findField("customerId").setValue(data.id);
            this.down('form').getForm().findField("customerName").setValue(data.company);
        }, this);
        customerWin.show();
    }
});